module.exports = function(sequelize, DataTypes) {
  var helper = require('../lib/merge')
  var rating = require('./mixins/RatingMixin')

  var Comment = sequelize.define(
    'Comment',
    helper.mergeWithArray(
      {
        id: {
          type: DataTypes.BIGINT,
          primaryKey: true,
          autoIncrement: true
        },
        data: {
          type: DataTypes.JSON,
          comment: 'The data {content, audios, images} of the comment',
          set: function(val) {
            this.setDataValue('data', val)
            this.setDataValue('hasData', val != null)
          }
        },
        hasData: {
          type: DataTypes.BOOLEAN,
          defaultValue: false,
          comment: 'likes and ratings may not have data'
        },
        liked: {
          type: DataTypes.TINYINT(1),
          defaultValue: 0,
          comment: 'liked +1 other 0'
        },
        userInfo: {
          type: DataTypes.JSON,
          comment: 'user info to be displayed with comment'
        },
        scope: {
          type: DataTypes.ENUM('task', 'progress', 'answer', 'document')
        },
        unreadCount: {
          type: DataTypes.TINYINT(1),
          defaultValue: 1,
          comment: 'New comments are unread'
        }
      },
      rating(DataTypes).attributes
    ),
    helper.mergeWithArray(
      {
        tableName: 'comments',
        paranoid: true,
        indexes: [
          {
            name: 'has_data_idx',
            fields: ['hasData']
          },
          {
            name: 'created_at_idx',
            fields: ['createdAt']
          },
          {
            name: 'scoped_id_idx',
            fields: ['targetId', 'scope', 'hasData', 'userId']
          },
          {
            name: 'unread_count_idx',
            fields: ['unreadCount']
          }
        ],
        comments: 'Comments for Answers and Progresses'
      },
      rating(DataTypes).options
    )
  )

  Comment.associate = function(models) {
    models.Task.hasMany(this, {
      foreignKey: 'targetId',
      constraints: false,
      scope: {
        scope: 'task'
      }
    })
    this.belongsTo(models.Task, {
      foreignKey: 'targetId',
      scope: {
        scope: 'task'
      },
      constraints: false
    })
    models.Progress.hasMany(this, {
      foreignKey: 'targetId',
      constraints: false,
      scope: {
        scope: 'progress'
      }
    })
    this.belongsTo(models.Progress, {
      foreignKey: 'targetId',
      scope: {
        scope: 'progress'
      },
      constraints: false
    })
    models.Answer.hasMany(this, {
      foreignKey: 'targetId',
      constraints: false,
      scope: {
        scope: 'answer'
      }
    })
    this.belongsTo(models.Answer, {
      foreignKey: 'targetId',
      scope: {
        scope: 'answer'
      },
      constraints: false
    })
    models.Document.hasMany(this, {
      foreignKey: 'targetId',
      constraints: false,
      scope: {
        scope: 'document'
      }
    })
    this.belongsTo(models.Document, {
      foreignKey: 'targetId',
      scope: {
        scope: 'document'
      },
      constraints: false
    })
    models.Comment.hasMany(this, {
      foreignKey: 'targetId',
      constraints: false,
      scope: {
        scope: 'comment'
      }
    })
    this.belongsTo(models.Comment, {
      foreignKey: 'targetId',
      scope: {
        scope: 'comment'
      },
      constraints: false
    })
    this.belongsTo(models.User, {
      as: 'Owner',
      foreignKey: 'userId',
      constraints: false
    })
  }
  return Comment
}
